<template>
    <div class="login-container">
        <el-form label-width="100" class="login-form">
            <el-form-item label="用户名">
                <el-input v-model="account" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码">
                <el-input v-model="password" type="password" placeholder="密码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :loading="loading" @click="login">登陆</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>

  import * as actionTypes from '../../constants/action-types'
  import utils from '../../utils/utils';

  export default {
    name: "login",
    data() {
      return {
        account: '',
        password: '',
        loading: false,
      }
    },
    computed: {},
    methods: {
      login(){
        if(!this.account || !this.password){
          utils.toast('请输入用户名和密码', 'error')
          return;
        }
        this.loading= true;
        this.$store.dispatch(actionTypes.LOGIN, {account: this.account, password:this.password}).then(()=>{
          this.loading= false;
          this.$store.dispatch(actionTypes.USERINFO);
          this.$router.push(this.$route.query.redirect || '/');
        }).catch(()=>{
          this.loading=false;
        })
      }
    },
    mounted() {

    },
    created() {

    },
  }
</script>

<style scoped>
    .login-container{
        width: 380px;
        padding-top: 100px;
        margin: 0 auto;
    }
</style>
